package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.HttpUtils;
import com.ruoyi.common.utils.MoneyUtil;
import com.ruoyi.common.utils.payment.pbqzpay.PubuPayUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Service("lakalaPayService")
public class LklPayServiceImpl extends BasePayment {




    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        String qrcode = userRecipientInfo.getReceiptQrcode();
//        String qrcode_ = HttpUtils.getParam(qrcode,"userAppId");
        String accountJson = userRecipientInfo.getAccountJson();
        JSONObject  accountObj = JSONObject.parseObject(accountJson);
        String token = accountObj.getString("ck");
        headerMap.put("Authorization",token);
        headerMap.put("X-Client-PV","lKL_APP");
        headerMap.put("Host","wallet.lakala.com");
        headerMap.put("User-Agent","lakala_app/56 CFNetwork/3826.400.120 Darwin/24.3.0");
        JSONObject  reqOrderObj  = new JSONObject();
        JSONObject reqDataObj = new JSONObject();
        reqDataObj.put("shopNo",accountObj.getString("lklShopno"));
        reqDataObj.put("shopName",accountObj.getString("lklShopname"));
        reqDataObj.put("termNo",accountObj.getString("lklTermno"));
        reqDataObj.put("type","MICROCODE");
        reqDataObj.put("expireTime","180");

        JSONObject orderFieldObj = new JSONObject();
        orderFieldObj.put("amount",orderInfo.getMoney());
        orderFieldObj.put("exterMerOrderNo","");
        orderFieldObj.put("exterOrderSource","");
        orderFieldObj.put("subject","");
        orderFieldObj.put("description","");
        orderFieldObj.put("orderRemark",orderInfo.getId()+"");
        reqDataObj.put("orderField",orderFieldObj);
        JSONObject txnFieldObj = new JSONObject();
        txnFieldObj.put("amount",orderInfo.getMoney());
        txnFieldObj.put("outTradeNo",orderInfo.getId()+"");
        txnFieldObj.put("operatorId","");
        txnFieldObj.put("remark",orderInfo.getId()+"");
        reqDataObj.put("txnField",txnFieldObj);
        reqDataObj.put("snAutoExpireFlag","");
        reqOrderObj.put("reqData", reqDataObj);
        reqOrderObj.put("ver","1.0.0");
        reqOrderObj.put("timestamp",sdf.format(new Date()));
        reqOrderObj.put("reqId","");
        reqOrderObj.put("rnd","");
        reqOrderObj.put("sign","");
        String submitPayUrl = "https://wallet.lakala.com/m/a/code/generate";
        logger.info("拉卡拉请求:{}",reqOrderObj.toJSONString());
        String submitPayRes = HttpUtils.sendJsonMessage(submitPayUrl,headerMap,reqOrderObj.toJSONString());
        JSONObject  submitPayResObj = JSONObject.parseObject(submitPayRes);
        if(!"000000".equals(submitPayResObj.getString("retCode"))){
            jsonObject.put("code","100");
            jsonObject.put("msg",submitPayResObj.getString("message"));
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientInfo.setDisabledInfo(submitPayResObj.getString("retMsg"));
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        logger.info("拉卡拉返回:{}",submitPayResObj.toJSONString());
        JSONObject payDataObj = submitPayResObj.getJSONObject("respData");
        String transaction_id = payDataObj.getString("sn");
        String payUrl = payDataObj.getString("url");
        jsonObject.put("payUrl",payUrl);
        jsonObject.put("orderNo",transaction_id);
        jsonObject.put("code","200");
        return jsonObject;
    }


    public JSONObject createWxPayOrder(TOrderInfo orderInfo) throws Exception {
        return createPayOrder(orderInfo);
    }

    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }
}
